package com.tica.it.itmanage.server.user.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tica.it.itmanage.core.domain.security.MenuEntity;
import com.tica.it.itmanage.core.domain.security.RoleMenuEntity;
import com.tica.it.itmanage.core.domain.security.UserEntity;
import com.tica.it.itmanage.core.domain.security.UserRoleEntity;
import com.tica.it.itmanage.core.dto.RoleMenuDTO;
import com.tica.it.itmanage.core.dto.UserDetailDTO;
import com.tica.it.itmanage.core.mapper.security.UserMapper;
import com.tica.it.itmanage.server.permission.PermissionService;
import com.tica.it.itmanage.server.role.RolePermissionService;
import com.tica.it.itmanage.server.user.UserService;
import com.tica.it.itmanage.server.user.userrole.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author hanbin
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    RolePermissionService rolePermissionService;

    /**
     * @param username
     * @return
     */
    @Override
    public UserDetailDTO getUserDetail(String username) {
        return null;
    }


    /**
     * @param username
     * @return
     */
    @Override
    public RoleMenuDTO getPermissionByUsername(String username) {
        RoleMenuDTO roleMenuDTO = new RoleMenuDTO();
//        List<MenuEntity> permissions = new ArrayList<>();
//        UserEntity user = super.getOne(Wrappers.<UserEntity>lambdaQuery().eq(UserEntity::getUsername, username), true);
//        if (null != user) {
//            List<UserRoleEntity> userRoles = userRoleService.list(Wrappers.<UserRoleEntity>lambdaQuery().eq(UserRoleEntity::getUserId, user.getId()));
//            if (CollectionUtils.isNotEmpty(userRoles)) {
//                List<Integer> roleIds = new ArrayList<>();
//                userRoles.stream().forEach(userRole -> {
//                    roleIds.add(userRole.getRoleId());
//                });
//                List<RoleMenuEntity> rolePermissions = rolePermissionService.list(Wrappers.<RoleMenuEntity>lambdaQuery().in(RoleMenuEntity::getRoleId, roleIds));
//                if (CollectionUtils.isNotEmpty(rolePermissions)) {
//                    List<Integer> permissionIds = new ArrayList<>();
//                    rolePermissions.stream().forEach(rolePermission -> {
//                        permissionIds.add(rolePermission.getPermissionId());
//                    });
//                    permissions = permissionService.list(Wrappers.<MenuEntity>lambdaQuery().in(MenuEntity::getId, permissionIds));
//                }
//            }
//        }
        return roleMenuDTO;
    }
}
